package com.cema.manage.common.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.enums.DBType;
import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

public class DataBaseUtils {
    public static String DRIVER_CLASS = "com.mysql.jdbc.Driver";

    public DataBaseUtils() {
    }

    public static DataSource getDataSources(String url, String userName, String password) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(userName);
        druidDataSource.setPassword(password);
        druidDataSource.setDriverClassName(DRIVER_CLASS);
        druidDataSource.setInitialSize(5);
        druidDataSource.setMaxActive(2000);
        druidDataSource.setMinIdle(1);
        druidDataSource.setMaxWait(60000L);
        druidDataSource.setTimeBetweenEvictionRunsMillis(60000L);
        druidDataSource.setMinEvictableIdleTimeMillis(300000L);
        druidDataSource.setValidationQuery("SELECT 1");
        druidDataSource.setTestWhileIdle(true);
        druidDataSource.setTestOnBorrow(false);
        druidDataSource.setTestOnReturn(false);
        druidDataSource.setPoolPreparedStatements(true);
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(20);

        try {
            if (null != druidDataSource) {
                druidDataSource.setFilters("wall,stat");
                druidDataSource.setUseGlobalDataSourceStat(true);
                druidDataSource.init();
            }

            return druidDataSource;
        } catch (Exception var5) {
            throw new RuntimeException("load datasource error, dbProperties is :", var5);
        }
    }

    public static MybatisSqlSessionFactoryBean getSessionFactory(DataSource dataSource, String packages, String xmlPath) throws IOException {
        MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
        mybatisPlus.setDataSource(dataSource);
        mybatisPlus.setVfs(SpringBootVFS.class);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        MybatisConfiguration mc = new MybatisConfiguration();
        mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        mc.setCallSettersOnNulls(true);
        mybatisPlus.setConfiguration(mc);
        mybatisPlus.setTypeAliasesPackage(packages);
        mybatisPlus.setMapperLocations(resolver.getResources(xmlPath));
        GlobalConfiguration globalConfig = new GlobalConfiguration(new LogicSqlInjector());
        globalConfig.setDbColumnUnderline(false);
        globalConfig.setDbType(DBType.MYSQL.getDb());
        globalConfig.setIdType(0);
        mybatisPlus.setGlobalConfig(globalConfig);
        return mybatisPlus;
    }
}
